<html>

<!--
  Copyright (c) 2006-2009 The Chromium Authors. All rights reserved.
  Use of this source code is governed by a BSD-style license that can be
  found in the LICENSE file.
-->

<head>
<style>
table {
  font-family: monospace;
  border-collapse: collapse;
}
thead {
  border-top: solid 1px gray;
  border-left: solid 1px gray;
}
tbody {
  border-top: solid 1px gray;
  border-bottom: solid 1px gray;
  border-left: solid 1px gray;
}
th {
  text-align: center;
  border-right: solid 1px gray;
}
td {
  text-align: right;
  padding-left: 2em;
  padding-right: 0.5em;
  border-right: solid 1px gray;
}
tr.sep {
  border-top: solid 1px gray;
  border-bottom: solid 1px gray;
}
td.max-value {
  color: red;
}
</style>
<script src="js/common.js"></script>
<script>
function get_index_of_max(ary) {
  var max = ary[0];
  var result = 0;
  for (var i = 1; i < ary.length; ++i) {
    if (ary[i] > max) {
      max = ary[i];
      result = i;
    }
  }
  return result;
}

function append_column(tr, value, sums, index) {
  td = document.createElement("TD");
  td.appendChild(document.createTextNode(value));
  tr.appendChild(td);

  if (index >= 0) {
    if (!sums[index])
      sums[index] = 0;
    sums[index] += parseFloat(value);
  }
}

function received_data(data) {
  var tbody = document.getElementById("tbody");
  data.replace('\r', '');

  var col_sums = [];
  var rows = data.split('\n');
  var num_rows = 0;

  for (var i = 0; i < rows.length; ++i) {
    var tr = document.createElement("TR");
    var cols = rows[i].split(' ');

    // cols[0] = page name
    // cols[1] = (mean+/-standard deviation):
    // cols[2...] = individual runs
    // Require at least the page name and statistics.
    if (cols.length < 2)
      continue;

    var page = cols[0];
    var values = cols[1].split('+/-');
    append_column(tr, page, col_sums, -1);
    append_column(tr, values[0].slice(1), col_sums, 0);
    append_column(tr, values[1].slice(0,-2), col_sums, 1);

    for (var j = 2; j < cols.length; ++j)
      append_column(tr, cols[j], col_sums, j);

    tbody.appendChild(tr);
    num_rows++;
  }

  // print out the column totals (highlight the max value)
  var index_of_max = get_index_of_max(col_sums);

  var tr = document.createElement("TR");
  tr.setAttribute("class", "sep");

  var td = document.createElement("TD");
  td.appendChild(document.createTextNode("column totals"));
  tr.appendChild(td);

  for (var j = 0; j < col_sums.length; ++j) {
    td = document.createElement("TD");
    // don't display the summation of the stddev column since it is bogus
    if (j != 1) {
      if (j == index_of_max)
        td.setAttribute("class", "max-value");
      var precision = j == 0 ? 2 : 0;
      td.appendChild(document.createTextNode(col_sums[j].toFixed(precision)));
    }
    tr.appendChild(td);
  }

  tbody.appendChild(tr);

  // print out the column averages
  var tr = document.createElement("TR");
  tr.setAttribute("class", "sep");

  var td = document.createElement("TD");
  td.appendChild(document.createTextNode("column averages"));
  tr.appendChild(td);

  for (var j = 0; j < col_sums.length; ++j) {
    td = document.createElement("TD");
    // don't display the average of the stddev column since it is bogus
    if (j != 1) {
      var precision = 2;
      td.appendChild(document.createTextNode(
          (col_sums[j]/num_rows).toFixed(precision)));
    }
    tr.appendChild(td);
  }

  tbody.appendChild(tr);
}

function init() {
  var params = ParseParams();
  var graph = params.graph ? params.graph + "_" : "";
  var cl = params.cl;
  var traces = [];
  for (var trace in params.trace) {
    // Try to fetch both files, because page cycler and frame rate have
    // have different output files (this should be fixed).
    Fetch(cl + "_" + trace + ".dat", received_data);
    Fetch(cl + "_" + graph + trace + ".dat", received_data);
    traces.push(trace);
  }
  if (traces.length > 0)
    document.getElementById("description").innerText = traces + " in r" + cl;
}

window.addEventListener("load", init, false);
</script>
</head>
<body>
<div id="description"></div>
<table>
  <thead>
    <tr><th>Data</th><th>Mean</th><th>StdDev</th><th colspan="10">Runs...</th></tr>
  </thead>
  <tbody id="tbody">
  </tbody>
</table>
</body>
</html>
